package org.apache.cordova.wu;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 扩展类
 * 
 * @author 吴Sir 
 * @date 16-05-20.  
 *
 */
public class PluginPlus {

	/**
	 * 返回默认值 null
	 * 
	 * @param obj
	 * @param key
	 * @return
	 */
	public String getString(JSONObject obj, String key) {
		return getString(obj, key, null);
	}

	public String getString(JSONObject obj, String key, String defaultValue) {
		try {
			return obj.getString(key);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return defaultValue;
		}
	}

	/**
	 * 返回默认值 null
	 * 
	 * @param obj
	 * @param key
	 * @return
	 */
	public Integer getInt(JSONObject obj, String key) {
		return getInt(obj, key, null);
	}

	public Integer getInt(JSONObject obj, String key, Integer defaultValue) {
		try {
			return obj.getInt(key);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return defaultValue;
		}
	}

	/**
	 * 返回默认值 null
	 * 
	 * @param obj
	 * @param key
	 * @return
	 */
	public Long getLong(JSONObject obj, String key) {
		return getLong(obj, key, null);
	}

	public Long getLong(JSONObject obj, String key, Long defaultValue) {
		try {
			return obj.getLong(key);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return defaultValue;
		}
	}

	/**
	 * 返回默认值 null
	 * 
	 * @param obj
	 * @param key
	 * @return
	 */
	public Double getDouble(JSONObject obj, String key) {
		return getDouble(obj, key, null);
	}

	public Double getDouble(JSONObject obj, String key, Double defaultValue) {
		try {
			return obj.getDouble(key);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return defaultValue;
		}
	}

	/**
	 * 返回默认值 false
	 * 
	 * @param obj
	 * @param key
	 * @return
	 */
	public Boolean getBoolean(JSONObject obj, String key) {
		return getBoolean(obj, key, false);
	}

	public Boolean getBoolean(JSONObject obj, String key, Boolean defaultValue) {
		try {
			return obj.getBoolean(key);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return defaultValue;
		}
	}

	public Object getObject(JSONObject obj, String key) {
		try {
			return obj.get(key);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	public JSONObject getJSONObject(JSONObject obj, String key) {
		try {
			return obj.getJSONObject(key);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	public JSONArray getJSONArray(JSONObject obj, String key) {
		try {
			return obj.getJSONArray(key);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

}
